<?
require "./controller/db.php";

$sql_count = "select count(*) from headlines";
$sql_all = "select * from headlines";

$keyword = $_GET['keyword'];
$selectArr = $_GET['selectArr'];
if ($keyword && $selectArr)
{
    if (!strpos($selectArr, 'content'))
    {
        $sql_where = " where title like '%$keyword%' ";
    }
    else
    {
        $sql_where = " where title like '%$keyword%' or content like '%$keyword%' ";
    }
}

$sql = "{$sql_count}  {$sql_where}";
$count = $db->query($sql)->fetch()[0];

$page = $_GET['page'];
$limit = 5;
$maxPage = $count / $limit + 1;

if ($page < 1 or $page > $maxPage)
{
    $page = 1;
}
$skip = ($page - 1) * $limit;
$sql_skip = "order by id desc limit {$skip},{$limit}";

$id = $_GET['id'];
if ($id)
{
    $sql = "select * from headlines where id = {$id} ";
}
else
{
    $sql = "{$sql_all} {$sql_where} {$sql_skip}";
}
$rows = $db->query($sql);
